﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.Cust.XH.PlugBE.Tool
{
    public static class DBHelper
    {
        public static DataTable getU9Datatble(string sql)
        {
            DataSet ds = null;
            int result = UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, null, true, out ds);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        /// <summary>
        /// 主表序列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static List<FileNV> getMainDtTONVList(DataTable dt, string id)
        {

            List<FileNV> row = new List<FileNV>();
            if (dt.Rows.Count > 0)
            {

                for (int i = 0; i < 1; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //此处ID取 私有段1 为记录的OA流程ID 如果存在添加ID   达到更新结果
                        if (dt.Columns[j].ColumnName == "id" || dt.Columns[j].ColumnName == "createcode")
                        {
                            continue;
                        }
                        //采购、付款需要上传附件
                        if (dt.Columns[j].ColumnName == "fj")
                        {
                            FileNV file = new FileNV(dt.Columns[j].ColumnName, FileInfoHelper.GetFileInfo(id));
                            row.Add(file);
                        }
                        else
                        {
                            FileNV file = new FileNV(dt.Columns[j].ColumnName, dt.Rows[i][j]);
                            row.Add(file);
                        }
                    }
                }
            }
            return row;
        }
        /// <summary>
        /// 子表新增
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="dtname"></param>
        /// <returns></returns>
        public static List<DetailDataModel> getChildDtTONVList(DataTable dt, string dtname)
        {

            List<DetailDataModel> l = new List<DetailDataModel>();
            if (dt != null)
            {
                DetailDataModel row = new DetailDataModel();
                row.tableDBName = dtname;
                row.workflowRequestTableRecords = new List<DetailDataModelChild>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DetailDataModelChild rows = new DetailDataModelChild();
                    rows.workflowRequestTableFields = new List<FileNV>();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //此处ID取 私有段1 为记录的OA流程ID 如果存在添加ID   达到更新结果
                        if (dt.Columns[j].ColumnName == "id")
                        {
                            continue;
                        }
                        FileNV file = new FileNV(dt.Columns[j].ColumnName, dt.Rows[i][j]);
                        rows.workflowRequestTableFields.Add(file);
                    }
                    row.workflowRequestTableRecords.Add(rows);

                }
                l.Add(row);
            }
            return l;
        }

        /// <summary>
        /// 提交流程接口 子表json拼接
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="dtname"></param>
        /// <returns></returns>
        public static List<DetailDataModel2> getChildDtTONVList(DataTable dt, string dtname, string deleteAll)
        {

            List<DetailDataModel2> l = new List<DetailDataModel2>();
            if (dt != null)
            {
                DetailDataModel2 row = new DetailDataModel2();
                row.deleteAll = deleteAll;
                row.tableDBName = dtname;
                row.workflowRequestTableRecords = new List<DetailDataModelChild>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DetailDataModelChild rows = new DetailDataModelChild();
                    rows.workflowRequestTableFields = new List<FileNV>();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //此处ID取 私有段1 为记录的OA流程ID 如果存在添加ID   达到更新结果
                        if (dt.Columns[j].ColumnName == "id")
                        {
                            continue;
                        }
                        FileNV file = new FileNV(dt.Columns[j].ColumnName, dt.Rows[i][j]);
                        rows.workflowRequestTableFields.Add(file);
                    }
                    row.workflowRequestTableRecords.Add(rows);

                }
                l.Add(row);
            }
            return l;
        }

        /// <summary>
        /// 获取OA流程ID
        /// </summary>
        /// <returns></returns>
        public static string getOAFlowID(string orgcode, string bill)
        {
            DataSet ds = null;
            string sql = $"exec proc_CustXH_Voucher '{orgcode}','{bill}'";
            int result = UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, null, true, out ds);
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return ds.Tables[0].Rows[0]["Description"].ToString();
                }
            }
            return "";
        }
    }


}
